package net.angrynut.web.handler.endpoint;

import com.google.common.collect.ImmutableMap;
import lombok.Setter;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 演示自定义 {@link Endpoint}。
 *
 * `endpoints.demoEndpoint.enabled` 为 `false` 时，`/demoEndpoint` 返回：
 *
 * ```json
 * { "message": "This endpoint is disabled" }
 * ```
 *
 * 为 `true` 时，返回 {@link #invoke()} 方法的结果。
 */
@ConfigurationProperties(prefix = "endpoints.demoEndpoint")
@Setter
@Component
public class DemoEndpoint implements Endpoint<Map<String, Object>> {

    private Boolean sensitive = false;

    private Boolean enabled = true;

    private String id = "demoEndpoint";

    @Override
    public String getId() {
        return id;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    @Override
    public boolean isSensitive() {
        return sensitive;
    }

    @Override
    public Map<String, Object> invoke() {
        return ImmutableMap.of("Hello", "Demo Endpoint", "sensitive", sensitive, "enabled", enabled);
    }
}
